package com.kady.controller.foodType;

import com.alibaba.fastjson.JSON;
import com.kady.contant.BeanFactoryConstant;
import com.kady.contant.ExceptionConstant;
import com.kady.contant.MessageConstant;
import com.kady.controller.BaseServlet;
import com.kady.entity.Food;
import com.kady.entity.FoodType;
import com.kady.entity.ResultVO;
import com.kady.factory.BeanFactory;
import com.kady.service.FoodService;
import com.kady.service.FoodTypeService;
import com.kady.service.impl.FoodServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/25
 * \* Time: 14:21
 * \* 千峰JAVA2101班
 * \* Description: 菜系servlet类
 * \
 */

@WebServlet("/backend/foodtype")
public class FoodTypeController extends BaseServlet {

    private FoodTypeService foodTypeService = (FoodTypeService)BeanFactory.getBean(BeanFactoryConstant.FOODTYPE_SERVICE);
    private FoodService foodService = (FoodService) BeanFactory.getBean(BeanFactoryConstant.FOOD_SERVICE);

    /**
     * 获取菜系列表方法
     */
    public String list (HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String keyword = req.getParameter("keyword");
        List<FoodType> list = foodTypeService.getFoodTypeList(keyword);

        /**
         *用于菜品表单菜系列表的获取
         */
        String patchUrl = req.getParameter("patchUrl");

        req.setAttribute("foodTypes", list);

        if(patchUrl==null){
            //菜系列表查询正常操作
            req.setAttribute("keyword", keyword==null?"":keyword.trim());
            return MessageConstant.PREFIX_FORWARD + "/backend/detail/foodtype/food-type-list.jsp";
        }else{
             //用于菜品模块修改菜品时的菜品信息查询  根据有无菜品ID查菜品信息并跳转添加/修改页面
            String foodId = req.getParameter("foodId");
            if(foodId!=null){
                Food findFood = new FoodServiceImpl().findById(foodId);
                if(findFood!=null){
                    req.setAttribute("food",findFood);
                }
            }
            return MessageConstant.PREFIX_FORWARD + patchUrl;
        }
    }

    /**
     * 搜索菜系的方法
     */
    public String findById (HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, SQLException {
        long typeId = Long.valueOf(req.getParameter("typeId"));
        FoodType resFoodType = foodTypeService.findOne(typeId);
        req.setAttribute("foodType",resFoodType);
        return MessageConstant.PREFIX_FORWARD + "/backend/detail/foodtype/food-type-update.jsp";
    }

    /**
     * 添加菜系的方法
     *
     */
    public Object save (HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, SQLException {
        String typeName =  req.getParameter("typeName");
        if(typeName.trim().isEmpty()){
            return new ResultVO(false, ExceptionConstant.TYPE_NAME_NULL_FAIL);
        }
        FoodType foodType = new FoodType();
        foodType.setTypeName(typeName);
        foodTypeService.addFoodType(foodType);
        return MessageConstant.PREFIX_FORWARD + "/backend/foodtype?method=list";
    }

    /**
     * 修改菜系的方法
     */
    public Object update (HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, SQLException {
        Map<String,String[]> paramMap =  req.getParameterMap();

        FoodType foodType = new FoodType();
        BeanUtils.populate(foodType,paramMap);
        foodTypeService.updateFoodType(foodType);
        return MessageConstant.PREFIX_FORWARD + "/backend/foodtype?method=list";
    }

    /**
     * 删除菜系的方法
     */
    public Object delete (HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, SQLException {
        long typeId = Long.valueOf(req.getParameter("typeId"));
        List<Food> findList = foodService.findByTypeId(typeId);
        if(findList.isEmpty()){
            foodTypeService.removeFoodType(typeId);
            return MessageConstant.PREFIX_FORWARD + "/backend/foodtype?method=list";
        }else{
            return new ResultVO(false,ExceptionConstant.FOODTYPE_DELETE_FAIL_MESSAGE);
        }
    }

    /**
     * 通过名称查询菜系
     */
    public String checkName(HttpServletRequest req,HttpServletResponse resp) throws SQLException {
        String name = req.getParameter("name");
        FoodType foodType = foodTypeService.getFoodTypeByName(name);
        if(foodType!=null){
            return JSON.toJSONString(foodType);
        }
        return null;
    }
}
